package pm

import (
	"context"
	"encoding/json"
	"ez/apps/pm/auto/mc"
	"gitee.com/dreamwood/ez-go/db/mgo"
	"gitee.com/dreamwood/ez-go/ez"
	"gitee.com/dreamwood/ez-go/ss"
	"time"
)

type Owner struct {
	mgo.BaseDoc `bson:"-" json:"-"`
	Id          int64      `json:"id" bson:"id,omitempty"`
	House       []*House   `json:"house" bson:"house"`       //所属房屋
	HouseIds    []int64    `json:"houseIds" bson:"houseIds"` //所属房屋
	Name        string     `json:"name" bson:"name"`         //姓名
	IdCard      string     `json:"idCard" bson:"idCard"`     //身份证号
	Mobile      string     `json:"mobile" bson:"mobile"`     //手机号
	Address     string     `json:"address" bson:"address"`   //地址
	Note        string     `json:"note" bson:"note"`         //备注
	CreateAt    time.Time  `json:"createAt" bson:"createAt"`
	UpdateAt    time.Time  `json:"updateAt" bson:"updateAt"`
	DeleteAt    *time.Time `json:"deleteAt" bson:"deleteAt"`
}

func (this *Owner) DocName() string { return "Owner" }
func (this *Owner) GetId() int64    { return this.Id }
func (this *Owner) SetId(id int64)  { this.Id = id }
func (this *Owner) Create() error {
	return this.GetFactory().Create(this)
}
func (this *Owner) Replace() error {
	return this.GetFactory().Replace(this)
}
func (this *Owner) Save() error {
	if this.Id == 0 {
		this.CreateAt = time.Now()
		this.UpdateAt = time.Now()
		return this.GetFactory().Create(this)
	} else {
		this.UpdateAt = time.Now()
		return this.GetFactory().Replace(this)
	}
}

// 伪删除
func (this *Owner) Delete() error {
	return this.GetFactory().Delete(this)
}
func (this *Owner) UnDelete() error {
	return this.GetFactory().UnDelete(this)
}

// 真删除
func (this *Owner) Destroy() error {
	return this.GetFactory().Destroy(this)
}
func (this *Owner) ToString() string {
	return string(this.ToBytes())
}
func (this *Owner) ToBytes() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *Owner) Serialize() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *Owner) UnSerialize(data []byte) []byte {
	ez.Try(json.Unmarshal(data, this))
	return data
}
func (this *Owner) LoadHouse() {
	if this.House == nil {
		this.House = make([]*House, 0)
	}
	if this.HouseIds == nil {
		this.HouseIds = make([]int64, 0)
	}
	if len(this.HouseIds) > 0 {
		this.House = make([]*House, 0)
		for _, v := range this.HouseIds {
			find, e := NewHouseCrud().FindId(v)
			if e == nil {
				this.House = append(this.House, find)
			}
		}
	}
}
func (this *Owner) ClearRelationsBeforeSave() mgo.Doc {
	this.House = nil
	return this
}
func neverUsed_Owner() {
	//导入ss包
	a := ss.M{}
	ez.Debug(a)
}

type OwnerAccessControl struct {
	Access  bool
	Message string
	Action  string //控制器Action,小写开头，如c,u,r,d
	Model   *Owner
	Session *ez.Session
}

func NewOwnerAccessControl(model *Owner, action string, session *ez.Session) *OwnerAccessControl {
	ctrl := &OwnerAccessControl{
		Access:  true,
		Model:   model,
		Action:  action,
		Session: session,
	}
	ez.DispatchToMany(mc.OwnerAccessControlEvent, ctrl, context.TODO())
	return ctrl
}
